package cz.martindobias.aladin;

import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CountyListAdapter implements ListAdapter {
    private static long AUTO_ID = 0;
    private Context context;

    public static class County {
        public long id;
        public String name;
        public String url;

        private County(String name, String url) {
            this.id = ++AUTO_ID;
            this.name = name;
            this.url = url;
        }
    }

    private List<County> counties = new ArrayList<County>(15);

    public CountyListAdapter(Context context, Cursor cursor) {
        this.context = context;
        while(!cursor.isAfterLast()) {
            this.counties.add(new County(cursor.getString(cursor.getColumnIndex(ContentTables.COLUMN_SEQUENCE)), cursor.getString(cursor.getColumnIndex(ContentTables.COLUMN_RESULT))));
            cursor.moveToNext();
        }
    }

    public boolean areAllItemsEnabled() {
        return true;
    }

    public boolean isEnabled(int i) {
        return true;
    }

    public void registerDataSetObserver(DataSetObserver dataSetObserver) {
    }

    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
    }

    public int getCount() {
        return this.counties.size();
    }

    public Object getItem(int i) {
        return this.counties.get(i);
    }

    public long getItemId(int i) {
        return this.counties.get(i).id;
    }

    public boolean hasStableIds() {
        return true;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView text;
        if(view != null) {
            text = (TextView) view;
        } else {
            text = new TextView(this.context);
        }
        text.setText(this.counties.get(i).name);
        text.setPadding(10, 5, 10, 5);
        text.setEnabled(true);
        return text;
    }

    public int getItemViewType(int i) {
        return 1;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return this.counties.isEmpty();
    }
}
